package org.smr.ministore.micro;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.blade.dao.base.BaseMapper;
import org.blade.utils.IdGenUtils;
import org.smr.ministore.dao.GoodsPriceMapper;
import org.smr.ministore.entities.GoodsPrice;
import org.smr.ministore.service.GoodsPriceService;
import org.blade.service.base.AbstractService;

import org.blade.utils.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
@Transactional(readOnly = false)
public class GoodsPriceMicroService extends AbstractService<Long,GoodsPrice> implements GoodsPriceService {

	@Autowired
	private GoodsPriceMapper mapper;

	@Autowired
    public GoodsPriceMicroService(GoodsPriceMapper mapper) {
        super(mapper);
    }

    @ResponseBody
	@RequestMapping(method = RequestMethod.POST, value = "/GoodsPrice/batchSave")
	public int batchSave(@RequestBody List<GoodsPrice> entities) {
        this.batchSaveAttatch(entities);
    	return mapper.batchInsert(entities);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/GoodsPrice/batchModify")
    public int batchModify(List<GoodsPrice> entities) {
        this.batchModifyAttatch(entities);
        return mapper.batchUpdate(entities);
    }


    @ResponseBody
    @RequestMapping(method = RequestMethod.PUT, value = "/GoodsPrice/save")
    public int save(@RequestBody GoodsPrice entity) {
        this.saveAttatch(entity);
    	return mapper.insert(entity);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.PATCH, value = "/GoodsPrice/updateById")
    public int updateById(@RequestBody GoodsPrice id) {
        this.modifyAttach(id);
   	 	return mapper.updateById(id);
    }

    @ResponseBody
    @Transactional(readOnly = true)
    @RequestMapping(method = RequestMethod.GET, value = "/GoodsPrice/queryById/{id}")
    public GoodsPrice queryById( @PathVariable("id")Long id ) {
    	return (GoodsPrice) mapper.selectById(id);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE, value = "/GoodsPrice/deleteById/{id}")
    public int deleteById( @PathVariable("id")Long id ) {
        return mapper.deleteById(id);
    }

    @Override
    public int deleteByIdLogical(Long id) {
        return 0;
    }

    @ResponseBody
    @Transactional(readOnly = true)
    @RequestMapping( method = RequestMethod.POST, value = "/GoodsPrice/paginate/{pageSize}/{pageNum}")
    public Pager<GoodsPrice> paginate( GoodsPrice entity, @PathVariable("pageSize")Long pageSize,  @PathVariable("pageNum")Long pageNum ) {
        Pager<GoodsPrice> pager = new Pager(pageSize, pageNum);
        List<GoodsPrice> list = this.mapper.pagination( entity, pager );
        pager.setEntities(list);
        return pager;
    }


    public Pager<Map<String, Object>> getGoodsPriceList(Long organId, Long typeId, Long brandId, String goodsName, Integer quote,
                                                       Long pageSize,
                                                       Long pageNum) {

        Pager<Map<String, Object>> pager = new Pager( pageSize, pageNum );
        pager.setEntities(this.mapper.getGoodsPricePagination( organId,  typeId,  brandId,  goodsName,  quote, pager));
        return pager;
    }

    @ResponseBody
    @Transactional(readOnly = true)
    @RequestMapping( method = RequestMethod.POST, value = "/GoodsPrice/getGoodsPriceList/{pageSize}/{pageNum}")
    public Pager<Map<String, Object>> getGoodsPriceList(@RequestBody Map<String, Object> params,
                                                       @PathVariable("pageSize") Long pageSize,
                                                       @PathVariable("pageNum") Long pageNum) {

        Long organId = MapUtils.getLong(params,"organId");
        Long typeId  = MapUtils.getLong(params,"typeId");
        Long brandId  = MapUtils.getLong(params,"brandId");
        String goodsName  = MapUtils.getString(params,"goodsName");
        Integer quote  = MapUtils.getInteger(params,"quote");
        Pager<Map<String, Object>> pager = new Pager( pageSize, pageNum );
        pager.setEntities(this.mapper.getGoodsPricePagination( organId,  typeId,  brandId,  goodsName,  quote, pager));
        return pager;
    }

    @ResponseBody
    @Transactional(readOnly = true)
    @RequestMapping( method = RequestMethod.POST, value = "/GoodsPrice/getGoodsPriceBy/{organId}/{goodsId}")
    public GoodsPrice getGoodsPriceBy(@PathVariable("organId")Long organId,
                                      @PathVariable("goodsId")Long goodsId) {
        return this.mapper.getGoodsPriceBy(organId, goodsId);
    }

    @Override
    public Long getId() {
        return IdGenUtils.getNextLongId();
    }
}
